package com.lsh.payment.worker.kafka.consumer;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;

import java.util.HashMap;
import java.util.Map;

//@Configuration
//@EnableKafka
public class KafkaReceiverConfig {

    @Value("${broker_ip}")
    public String broker_ip;
    @Value("${group_id}")
    private String group_id;

    @Value("${enable_auto_commit}")
    private String enable_auto_commit;

    @Value("${auto_commit_interval_ms}")
    private String auto_commit_interval_ms;
    @Value("${request_timeout_ms}")

    private String request_timeout_ms;
    @Value("${session_timeout_ms}")
    private String session_timeout_ms;

    @Value("${auto_offset_reset}")
    private String auto_offset_reset;

    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        factory.setConcurrency(3);
        factory.getContainerProperties().setPollTimeout(10000);
        return factory;
    }

    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs());
    }


    @Bean
    public Map<String, Object> consumerConfigs() {
        Map<String, Object> propsMap = new HashMap<>();

        propsMap.put(ConsumerConfig.GROUP_ID_CONFIG, this.group_id);
        propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, this.broker_ip);
        propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, this.enable_auto_commit);
        propsMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, this.auto_offset_reset);
        propsMap.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, this.auto_commit_interval_ms);
        propsMap.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, this.session_timeout_ms);
        propsMap.put(ConsumerConfig.REQUEST_TIMEOUT_MS_CONFIG, this.request_timeout_ms);
        propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);

        return propsMap;
    }

    @Bean
    public KafkaReceiver kafkaReceiver() {
        return new KafkaReceiver();
    }

}
